package se.lth.eit.eit060.proj2.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import se.lth.eit.eit060.proj2.initiation.Utility;
import se.lth.eit.eit060.proj2.server.Logger;

public class MyPanel extends JPanel{
	private JPasswordField ps;
	private JTextField tf;
	private JTextField rc;
	private JTextField host;
	private JPasswordField myPassword;
	//private JTextField record;
	private JComboBox jc = new JComboBox(new String[]{Utility.CREATE, Utility.DELETE, Utility.LIST,Utility.READ, Utility.UPDATE});
	private int failed;
	private JTextArea ta;
	private MyClient mc;
	private JList list;
	private GUI g;
	/*public void login(String name, char[] pwd){
		mc.loginAndConnect(name, pwd);
		accepted();
	}
	public void accepted(){
		m.accepted();
	}*/
	public static void main(String[] e){
		MyPanel mp = new MyPanel();
	}
	public MyPanel(){
		setBackground(new Color(120, 120, 180));
		this.g = new GUI();
		list = new JList();
		host = new JTextField();
		myPassword = new JPasswordField();
		JPanel options = new JPanel(new GridLayout(12,1));
		setLayout(new GridLayout(1,3));
		ta = new JTextArea();
		ps = new JPasswordField();
		tf = new JTextField();
		rc = new JTextField();
		JButton b = new JButton("OK");
		b.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(mc==null){
					setHost();
				}
				mc.login(getUsername(), getPassword());
				if(list.getSelectedValue()!=null){
					mc.setActions(list.getSelectedValue().toString(),jc.getSelectedItem().toString(),new String(myPassword.getPassword()));
				}else{
					mc.setActions(null,jc.getSelectedItem().toString(),new String(myPassword.getPassword()));
					System.out.println(new String(myPassword.getPassword()));
				}
				if(!mc.isAlive()){
					mc.start();
				}
			}});
		options.add(new JLabel("Truststore:"));
		options.add(tf);
		tf.setText("john.jks");
		options.add(new JLabel("Truststore password:"));
		options.add(ps);
		ps.setText("password");
		/*options.add(new JLabel("Record:"));
		options.add(rc);*/
		options.add(new JLabel("Action:"));
		options.add(jc);
		options.add(new JLabel("Server password:"));
		options.add(myPassword);
		options.add(new JLabel("Host:"));
		host.setText("localhost");
		options.add(host);
		options.add(b);
		options.add(new JLabel("Failed: "+failed));
		add(ta);
		add(list);
		add(options);
		g.setVisible(this);
	}
	public void setHost(){
		mc = new MyClient(host.getText(),this);
	}
	
	public void setText(String[] orders){
		ta.setText("");
		for(String s : orders){
			ta.append(s+Logger.n);
		}
	}
	public String getText(){
		return ta.getText();
	}
	
	public void setFailed(int i){
		failed = i;
	}
	public String getRecord(){
		return rc.getText();
	}

	public char[] getPassword(){
		return ps.getPassword();
	}
	
	public String getUsername() {
		return tf.getText();
	}
	public void setList(String[] records) {
		list.setListData(records);
	}
	public void setTitle(String substring) {
		g.setTitle(substring);
	}
}